package com.neo.push.server.connection;

import com.neo.push.core.RegisterMessage;
import com.neo.push.core.connection.ConnectionMonitor;
import com.neo.push.core.connection.SocketConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by lock on 17-7-31.
 */
public class BaseConnectionMonitor implements ConnectionMonitor {



    private final Map<String,SocketConnection> connectionContext = new ConcurrentHashMap<>();
    private Logger logger = LoggerFactory.getLogger(BaseConnectionMonitor.class);


    /**
     * 找到当前连接
     * @param connectionId      连接的唯一标识
     * @return
     */
    @Override
    public SocketConnection findConnection(String connectionId) {
        return connectionContext.get(connectionId);
    }

    /**
     * 进行连接的注册
     * @param connection            哪个连接
     * @param registerMessage       注册信息
     */
    @Override
    public void register(SocketConnection connection, RegisterMessage registerMessage) {
        logger.info(" register message is :"+registerMessage.getTargetName());
        connectionContext.putIfAbsent(connection.id(),connection);
    }

    /**
     * 如果连接关闭了，那么则移除连接
     * @param connection            哪个链接被关闭
     */
    @Override
    public void closed(SocketConnection connection) {
        connectionContext.remove(connection.id());
    }


}
